package com.zyw.codegen;

import com.zyw.mvc.context.SpringContextHolder;
import org.mybatis.generator.api.ConnectionFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author zhangyw
 * @date 2017/3/13 16:03
 */
public class DataSourceConnectionFactory implements ConnectionFactory {

    private DataSource dataSource;

    public DataSourceConnectionFactory() {
    }

    public DataSourceConnectionFactory(DataSource dataSource) {
        super();
        this.dataSource = dataSource;
    }

    @Override
    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        this.dataSource = SpringContextHolder.getBean(properties.getProperty("lookupKey"));
    }
}
